package com.paireach.bw.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.paireach.bw.R;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.base.BaseActivity;
import com.paireach.bw.bean.TraceAlarmInfo;
import com.paireach.bw.common.Constant;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.UserRequest;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.BDTraceUtil;
import com.paireach.bw.utils.GsonUtil;
import com.paireach.bw.utils.LogUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "ChangePasswordActivity";
    Button button;
    EditText et1, et2;
    ImageView iv;
    private EditText et_fence_id;
    private EditText et_monitoredPersons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            Transition slide_bottom = TransitionInflater.from(this).inflateTransition(android.R.transition.slide_bottom);
            getWindow().setEnterTransition(slide_bottom);
        }

        setContentView(R.layout.activity_change_password);
        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        button = (Button) findViewById(R.id.buttonPanel);
        button.setOnClickListener(this);
        iv = (ImageView) findViewById(R.id.layout_top_back);
        iv.setOnClickListener(this);
        ((TextView) findViewById(R.id.layout_top_change)).setText("设置密码");

        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                method1();
            }
        });


        et_fence_id = (EditText) findViewById(R.id.et_fence_id);
        et_monitoredPersons = (EditText) findViewById(R.id.et_monitoredPersons);
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fence_id = et_fence_id.getText().toString().trim();
                String monitored_persons = et_monitoredPersons.getText().toString().trim();
                BDTraceUtil.queryFenceHistoryAlarmInfo(Integer.parseInt(fence_id), monitored_persons);
            }
        });
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BDTraceUtil.createCircularFence();
            }
        });
        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new WayBillRequest().saveFenceAlarmInfo(phoneNumber,"宏象1_18100000001", 2, 1486719379, 10, "fence_name", 121.49059665067931, 31.33904001141602,
                        3, 5, 121.49054677796136, 31.338977768927657, 3, 1486719374, 7);
            }
        });

        final String json = "{\"fence_id\":10,\"fence\":\"围栏名称1\",\"monitored_person\":\"宏象1_18100000001\",\"action\":2,\"time\":1486719379,\"longitude\":121.49059665067931,\"latitude\":31.33904001141602,\"coord_type\":3,\"radius\":5,\"pre_point\":{\"longitude\":121.49054677796136,\"latitude\":31.338977768927657,\"coord_type\":3,\"time\":1486719374,\"radius\":7}}";
        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TraceAlarmInfo info = GsonUtil.jsonToBean(json, TraceAlarmInfo.class);
                LogUtil.d(TAG, "action=" + info.getAction() + "--fence=" + info.getFence());
            }
        });
        findViewById(R.id.btn6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BDTraceUtil.queryMonitoredStatus(17);
            }
        });

        findViewById(R.id.btn7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BDTraceUtil.queryHistoryTrack();
            }
        });
    }

    private void method1() {
        MultiImageSelector.create(ChangePasswordActivity.this)
                .showCamera(true) // 是否显示相机. 默认为显示
                .count(3) // 最大选择图片数量, 默认为9. 只有在选择模式为多选时有效
                .single() // 单选模式
                .multi() // 多选模式, 默认模式;
                .start(ChangePasswordActivity.this, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                // 获取返回的图片列表
                List<String> path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                // 处理你自己的逻辑 ....
                LogUtil.d(TAG, "path=" + path.toString());
            }
        }
    }

    @Override
    public void onClick(View view) {
        if (view == button) {
            String pass1 = et1.getText().toString();
            String pass2 = et2.getText().toString();
            if (TextUtils.isEmpty(pass1) || TextUtils.isEmpty(pass2)) {
                showToast("密码不能为空");
            } else if (!pass1.equals(pass2)) {
                showToast("两次输入的密码不一致");
            } else {
                showProgressDialog();
                new UserRequest().changePassword(MyApplication.getSpUtils().getString(Constant.TEL), pass1);
            }
        } else if (view == iv) {
            onBackPressed();
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.CHANGE_PASSWORD) {
            dismissProgressDialog();
            showToast(event.getErrorMessage());
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.CHANGE_PASSWORD) {
            dismissProgressDialog();
            if (event.getJson().equals("0")) {
                showToast("密码修改失败");
            } else {
                showToast("密码修改成功");
                startActivity(new Intent(ChangePasswordActivity.this, LoginActivity.class));
                finish();
            }
        }
    }
}
